package com.example.springbootv1.controller;

import com.example.springbootv1.pojo.Bills;
import com.example.springbootv1.service.BillsService;
import com.example.springbootv1.util.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/bills")
@CrossOrigin("*")
public class BillsController {
    @Autowired
    private BillsService billsService;

    /**
     * 查询列表
     * @return Result
     */
    @GetMapping("/list")
    public Result getList() {
        return Result.success(billsService.getList());
    }

    /**
     * 从URL中获取参数需要 @PathVariable
     * @param id
     * @return Result
     */
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id) {
        return Result.success(billsService.queryById(id));
    }

    /**
     * 新增  从前端获取对象，需要@RequestBody
     * @return Result
     */
    @PostMapping("/add")
    public Result add(@RequestBody Bills bills) {
        return Result.success(billsService.insert(bills));
    }

    /**
     * 更新数据
     * @param  bills 对象
     * @return Result
     */
    @PutMapping("/update")
    public Result update(@RequestBody Bills bills) {
        return Result.success(billsService.update(bills));
    }

    /**
     * 删除日志
     * @param id ID
     * @return Result
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        return Result.success(billsService.deleteById(id));
    }

    /**
     * 登录验证    多个参数传递到XML里面的方法
     * @param name  登录名
     * @param charType  登录方法
     * @return 日志对象
     */
    @PostMapping("/login")
    public Result login(@RequestParam(value = "name", required = false) String name,
                        @RequestParam(value = "charType", required = false) String charType) {
        System.out.println("--------"+name+"-------------");
        System.out.println("--------"+charType+"-------------");
        Bills bills = billsService.login(name, charType);
        if (bills != null) {
            return Result.success(bills);
        } else {
            return Result.error("登录失败！");
        }

    }

    /**
     * 分页查询
     * @param bills 对象
     * 前端传回来：当前页和每页显示记录数
     * mapper层需要啥：
     * 1. 从第几条开始查
     * 2. 每页显示的记录数 （当前页-1）*每页显示的记录数
     * @return Result
     * @serialData 对象列表
     */
    @PostMapping("/fenYeList")
    public Result getLoginLogByFenYe(@RequestBody Bills bills) {
        int fromIndex = (bills.getCurrentIndex() - 1) * bills.getPageSize();
        bills.setFromIndex(fromIndex);
        Map<String , Object> map = new HashMap<String, Object>();
        map.put("pojoTestList", billsService.getByFenYe(bills));
        map.put("totalCount", billsService.getByFenYeCount(bills));
        return Result.success(map);
    }

    @GetMapping("/fenYeCount")
    public Result getByFenYeCount(Bills bills) {
        return Result.success(billsService.getByFenYeCount(bills));
    }

    /**
     * 搜索，模糊匹配
     * @param bills
     * @return
     */
    @GetMapping("/search")
    public Result getObjByObj(Bills bills) {
        List<Bills> search = billsService.getObjByObj(bills);
        if (!search.isEmpty()) {
            return Result.success(search);
        } else {
            return Result.error("没查到！");
        }

    }

    @DeleteMapping("/deleteBatch/{ids}")
    public Result deleteBatch(@PathVariable List<Long> ids) {
        int count = billsService.deleteBatch(ids);
        if (count > 0) {
            return Result.success(count);
        } else {
            return Result.error("删除失败！");
        }
    }
    
}
